package ufrgs.inf.delphos.identification;

import java.util.ArrayList;
import java.util.Hashtable;

import ufrgs.inf.delphos.extraction.model.*;
import ufrgs.inf.delphos.identification.knowledgebase.*;


public class IdentificationAlgorithm {

	private DSystem systemModel;
	private KBase knowledgebase;
	private Hashtable<Label, ArrayList<DCompilationUnit>> kbTable;
	
	
	public IdentificationAlgorithm(DSystem systemModel, KBase knowledgebase) {
		
		this.systemModel = systemModel;
		this.knowledgebase = knowledgebase;
	}
	
	public void putTagsKBTable(Tag tag) {
		
		if(! kbTable.containsKey(tag))
			kbTable.put(tag, new ArrayList<DCompilationUnit>());
		
		for(Artifact artifact: tag.getArtifacts())
			if(! kbTable.containsKey(artifact))
				kbTable.put(artifact, new ArrayList<DCompilationUnit>());
		
		for(Tag t: tag.getTags()) 			
			putTagsKBTable(t);
	}	
	
	public void createKBTable() {

		/* create a hash table with tags and artifacts */
		for(Goal goal: knowledgebase.getGoals()) {
			for(Indicator indicator: goal.getIndicators()) {
				for(Tag tag: indicator.getTags()) {
					putTagsKBTable(tag);
				}
			}
		}
	}
	
	public void getArtifactsFromDPackage(DPackage dpackage) {
		
		for(DCompilationUnit compUnit: dpackage.getCompUnits()) {
			
			for(DAnnotation annotation: compUnit.getAnnotations()) {
				
				switch(annotation.getAnnotationType()) {
				
					case DECLARE_ROLES:
						
						break;
					
					case DENY_ALL:
						break;
						
					case PERMIT_ALL:
						break;
						
					case ROLES_ALLOWED:
						break;
						
					case RUN_AS:
						break;
				
					default:
						break;
				}
				
				
			}
			
			for(DRelation relation: compUnit.getRelations()) {
				
				switch(relation.getRelationType()) {
				
					case CALL:
						break;
						
					case EXCEPTION:
						break;
						
					case IMPORT:
						break;
						
					case INHERITANCE:
						break;
						
					case INTERFACE:
						break;
						
					default:
						break;
				
				}
				
			}
		}
			
		for(DPackage p: dpackage.getPackages())
			getArtifactsFromDPackage(p);
	}
	
	
	public void getArtifactsFromSystemModel() {
		
		for(DPackage dpackage: systemModel.getPackages())
			getArtifactsFromDPackage(dpackage);
	}
	
	
	
	public void run() {
		
		createKBTable();		
		
		getArtifactsFromSystemModel();
		
		
		
		
	}
	
	
}
